﻿using AccustomeAttributedll;
using AixCommInfo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FinalTestMachine.Funs;
using LanguageControlBase.Wpf;

namespace FinalTestMachine.View.MainUnits
{
    /// <summary>
    /// Unit_FunsBaseSetting.xaml 的交互逻辑
    /// </summary>
    public partial class Unit_FunsBaseSetting : UserControl
    {
        public Unit_FunsBaseSetting()
        {
            InitializeComponent();
        }
        public void SetFunsSelections(FunsSelectionHelperbase model)
        {
            //先清理，再添加新数据
            gd_1.Children.Clear();
            Type type = model.GetType();

            var memberInfos = type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(s => s.IsDefined(typeof(FunctionSelectionRemarkAttribute), true) == true);
            int rowIndex = 0;

            foreach (var item in memberInfos)
            {
                if (item.MemberType == MemberTypes.Field)
                {
                    FieldInfo fieldInfo = item as FieldInfo;
                    object singleMsgSource = fieldInfo.GetValue(model);
                    FunctionSelectionRemarkAttribute paramRemarkAttribute = (FunctionSelectionRemarkAttribute)item.GetCustomAttributes(typeof(FunctionSelectionRemarkAttribute), true)[0];


                    //添加行
                    RowDefinition rowDefinition = new RowDefinition();
                    rowDefinition.Height = GridLength.Auto;
                    gd_1.RowDefinitions.Add(rowDefinition);
                    rowIndex = gd_1.RowDefinitions.IndexOf(rowDefinition);
                    LanTextBlock textBlock = new LanTextBlock();
                    string tempRemark = paramRemarkAttribute.GetRemark();
                    tempRemark = LanguageManager.LanguageSearchHelper.GetLanguage_UI(tempRemark);
                    //if (!tempRemark.Contains(':'))
                    //{
                    //    tempRemark = textBlock.Text + "：";
                    //}
                    textBlock.LanText = tempRemark;
                    textBlock.FontSize = 15;
                    gd_1.Children.Add(textBlock);
                    textBlock.HorizontalAlignment = HorizontalAlignment.Right;
                    textBlock.VerticalAlignment = VerticalAlignment.Center;
                    Grid.SetRow(textBlock, rowIndex);
                    Grid.SetColumn(textBlock, 0);


                    //初始化控件
                    if (fieldInfo.FieldType.Name == typeof(ParamStructText<>).Name)
                    {
                        paramRemarkAttribute.SetControlIntial(bool.Parse(((ParamStructText<bool>)singleMsgSource).GetValue.ToString()));

                    }
                    else if (fieldInfo.FieldType == typeof(bool))
                    {
                        paramRemarkAttribute.SetControlIntial(bool.Parse(singleMsgSource.ToString()));
                    }
                    else
                    {
                        continue;
                    }
                    //添加设置控件,一个勾选框
                    Control control = paramRemarkAttribute.GetControl();

                    control.Height = 25;
                    control.Width = 200;
                    control.FontSize = 15;
                    control.Margin = new Thickness(0, 5, 5, 5);
                    control.VerticalContentAlignment = VerticalAlignment.Bottom;
                    control.HorizontalAlignment = HorizontalAlignment.Left;
                    gd_1.Children.Add(control);
                    Grid.SetRow(control, rowIndex);
                    Grid.SetColumn(control, 2);

                    //设置事件
                    paramRemarkAttribute.actionUseTrigger += () =>
                    {
                        if (fieldInfo.FieldType.Name == typeof(ParamStructText<>).Name)
                        {
                            ParamStructText<bool> msgText = singleMsgSource as ParamStructText<bool>;
                            msgText.GetValue = true;
                        }
                        else
                        {
                            Type tt = (fieldInfo.GetValue(model)).GetType();
                            fieldInfo.SetValue(model, Convert.ChangeType(true, tt));
                        }
                    };

                    paramRemarkAttribute.actionLimitUseTrigger += () =>
                    {
                        if (fieldInfo.FieldType.Name == typeof(ParamStructText<>).Name)
                        {
                            ParamStructText<bool> msgText = singleMsgSource as ParamStructText<bool>;
                            msgText.GetValue = false;
                        }
                        else
                        {
                            Type tt = (fieldInfo.GetValue(model)).GetType();
                            fieldInfo.SetValue(model, Convert.ChangeType(false, tt));
                        }

                    };
                }



            }

        }


        //public void SetFunsSelections(Funs.FunsSelectionHelperbase model)
        //{
        //    this.gd_1.Children.Clear();
        //    Type type = model.GetType();

        //    var memberInfos = type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(s => s.IsDefined(typeof(FunctionSelectionRemarkAttribute), true) == true);
        //    int rowIndex = 0;

        //    foreach (var item in memberInfos)
        //    {
        //        if (item.MemberType == MemberTypes.Field)
        //        {
        //            FieldInfo fieldInfo = item as FieldInfo;
        //            object singleMsgSource = fieldInfo.GetValue(model);
        //            FunctionSelectionRemarkAttribute paramRemarkAttribute = (FunctionSelectionRemarkAttribute)item.GetCustomAttributes(typeof(FunctionSelectionRemarkAttribute), true)[0];


        //            //添加行
        //            RowDefinition rowDefinition = new RowDefinition();
        //            rowDefinition.Height = GridLength.Auto;
        //            gd_1.RowDefinitions.Add(rowDefinition);
        //            rowIndex = gd_1.RowDefinitions.IndexOf(rowDefinition);
        //            TextBlock textBlock = new TextBlock();
        //            string tempRemark = paramRemarkAttribute.GetRemark();
        //            tempRemark = LanguageManager.LanguageSearchHelper.GetLanguage_UI(tempRemark);
        //            //if (!tempRemark.Contains(':'))
        //            //{
        //            //    tempRemark = textBlock.Text + "：";
        //            //}
        //            textBlock.Text = tempRemark;
        //            textBlock.FontSize = 15;
        //            gd_1.Children.Add(textBlock);
        //            textBlock.HorizontalAlignment = HorizontalAlignment.Right;
        //            textBlock.VerticalAlignment = VerticalAlignment.Center;
        //            Grid.SetRow(textBlock, rowIndex);
        //            Grid.SetColumn(textBlock, 0);


        //            //初始化控件
        //            if (fieldInfo.FieldType.Name == typeof(ParamStructText<>).Name)
        //            {
        //                paramRemarkAttribute.SetControlIntial(bool.Parse(((ParamStructText<bool>)singleMsgSource).GetValue.ToString()));

        //            }
        //            else if (fieldInfo.FieldType == typeof(bool))
        //            {
        //                paramRemarkAttribute.SetControlIntial(bool.Parse(singleMsgSource.ToString()));
        //            }
        //            else
        //            {
        //                continue;
        //            }
        //            //添加设置控件
        //            Control control = paramRemarkAttribute.GetControl();

        //            control.Height = 25;
        //            control.Width = 200;
        //            control.FontSize = 15;
        //            control.Margin = new Thickness(0, 5, 5, 5);
        //            control.VerticalContentAlignment = VerticalAlignment.Bottom;
        //            control.HorizontalAlignment = HorizontalAlignment.Left;
        //            gd_1.Children.Add(control);
        //            Grid.SetRow(control, rowIndex);
        //            Grid.SetColumn(control, 2);

        //            //设置事件
        //            paramRemarkAttribute.actionUseTrigger += () =>
        //            {
        //                if (fieldInfo.FieldType.Name == typeof(ParamStructText<>).Name)
        //                {
        //                    ParamStructText<bool> msgText = singleMsgSource as ParamStructText<bool>;
        //                    msgText.GetValue = true;
        //                }
        //                else
        //                {
        //                    Type tt = (fieldInfo.GetValue(model)).GetType();

        //                    fieldInfo.SetValue(model, Convert.ChangeType(true, tt));

        //                }


        //            };

        //            paramRemarkAttribute.actionLimitUseTrigger += () =>
        //            {
        //                if (fieldInfo.FieldType.Name == typeof(ParamStructText<>).Name)
        //                {
        //                    ParamStructText<bool> msgText = singleMsgSource as ParamStructText<bool>;
        //                    msgText.GetValue = false;
        //                }
        //                else
        //                {
        //                    Type tt = (fieldInfo.GetValue(model)).GetType();

        //                    fieldInfo.SetValue(model, Convert.ChangeType(false, tt));
        //                }


        //            };
        //        }



        //    }

        //}

    }
}
